package org.vectrics.recruiter.struts.admin.file;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.vectrics.common.util.FileUtil;

/**
 * @author pgoette
 *
 */
public class FileForm extends ActionForm {
     
	private static final long serialVersionUID = 8590354974358262965L;

	/**
	 * id de persona
	 */
	private String idPerson = null;
	
	/**
	 * Nombre que se le va a dar al archivo.
	 */
	private String name = null;
	
	
	/**
	 * categoria
	 */
	private String category = null;
	
	/**
	 * Archivo
	 */
	private org.apache.struts.upload.FormFile theFile = null;
	
	/**
	 * @return the idPerson
	 */
	public String getIdPerson() {
		return idPerson;
	}

	/**
	 * @param idPerson the idPerson to set
	 */
	public void setIdPerson(String idPerson) {
		this.idPerson = idPerson;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the category
	 */
	public String getCategory() {
		return category;
	}

	/**
	 * @param category the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	/**
	 * @return the file
	 */
	public org.apache.struts.upload.FormFile getTheFile() {
		return theFile;
	}

	/**
	 * @param file the file to set
	 */
	public void setTheFile(org.apache.struts.upload.FormFile file) {
		this.theFile = file;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		// Valido que el file no sea nulo 
		if ((this.getTheFile()==null)||(this.getTheFile().getFileSize()==0)) {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.required"));
		} else {			
			// Valido el que el size no sea mayor de 60M 
			if (this.getTheFile().getFileSize() > 60000000) {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.size.max"));
			}
			
			String fileName = this.getTheFile().getFileName();
			// Se valida el tipo de archivo
			if (fileName.endsWith("pdf")) {
			}
			else if (fileName.endsWith("txt")) {
			}
			else if (fileName.endsWith("doc")) {
			}
			else {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.type"));
			}
		}
		
		// Valido que el nombre 
		if ((this.getName() == null)||(this.getName().trim().equals(""))) {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required.name"));
		} else {
			if (!FileUtil.isValidName(this.getName())) {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.name.is.invalid"));
			}
		}
		return errors;
	}
		
}
